#include "map.h"
#include "log.h"
#include <limits>

cMap::cMap()
{
    m_player = new cPlayer();
    m_mapTime = 0;
    m_mapTimer.start();
    //m_bullets.max_load_factor(std::numeric_limits<float>::infinity());

    log(LOG_INFO, "Map loaded\n");
}

cMap::~cMap()
{
    delete m_player;

    log(LOG_INFO, "Map ended\n");
}

void cMap::tick(int frameTime)
{
    m_mapTime = m_mapTimer.getTicks() / 10;

    //log(LOG_DEBUG, "maptime %d\n", m_mapTime);

    m_player->update(frameTime);

    std::unordered_set<cBullet*>::iterator i;
    for(i = m_bullets.begin(); i != m_bullets.end(); i++)
        if (!(*i)->disabled())
            (*i)->update(frameTime);
}

void cMap::createBullet(int x, int y, int speed, int direction)
{
    cBullet* bullet = new cBullet(x, y, speed, direction);
    m_bullets.insert(bullet);
}

void cMap::destroyBullet(cBullet* bullet)
{

    m_bullets.erase(bullet);
    delete bullet;
}

int cMap::time()
{
    return m_mapTime;
}

cPlayer* cMap::player()
{
    return m_player;
}

std::unordered_set<cBullet*> cMap::bullets()
{
    return m_bullets;
}
